package zyb.myproject;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Iterator;
import java.util.Scanner;

public class AdminOperateImpl implements AdminOperate {

	// 管理员登陆
	public void login() throws IOException, ClassNotFoundException {

		Scanner sc = new Scanner(System.in);
		System.out.print("请输入管理员账号：");
		String username = sc.next();
		System.out.print("请输入管理员密码：");
		int password = sc.nextInt();

		File file = new File("F:\\manager\\usermes\\" + username);

		if (file.exists()) {
			FileInputStream fis = new FileInputStream(file);
			ObjectInputStream ois = new ObjectInputStream(fis);
			Administrator admin = (Administrator) ois.readObject();

			if (username.equals(admin.getUsername()) && password == admin.getPassword()) {
				System.out.println("恭喜您，管理员" + admin.getName() + "登陆成功!");
			} else {
				System.out.println("密码错误，请重新登陆！");
				login();
			}
			fis.close();
			ois.close();
		} else {
			System.out.println("用户名错误，登陆失败");
			login();
		}

	}

	// 管理员进行商品上架
	@Override
	public void addGoods() throws IOException {

		boolean b = true;
		do {
			Scanner sc = new Scanner(System.in);
			System.out.print("请输入商品编号：");
			int id = sc.nextInt();
			System.out.print("请输入商品名称：");
			String goodname = sc.next();
			System.out.print("请输入商品价格：");
			double price = sc.nextDouble();
			System.out.print("请输入商品数量：");
			int num = sc.nextInt();
			Goods goods = new Goods(id, goodname, price, num);
			FileOutputStream fos = new FileOutputStream("F:\\manager\\goods\\" + goods.getGoodName() + ".dat");
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(goods);
			System.out.println("添加完成。");
			fos.close();
			oos.close();
			System.out.println("请问是否继续进行商品上架：y/n");
			String n = sc.next();

			if ("n".equals(n)) {
				b = false;
			}

		} while (b);

	}

	// 根据购物车的商品修改 库存数量
	@Override
	public void changeGoods(Cart cart) throws IOException, ClassNotFoundException {

		// Iterator<Goods> iter = cart.list.iterator();
		// while (iter.hasNext()) {
		// Goods goods = iter.next();
		// System.out.println("商品名称：" + goods.getGoodName() + ",商品价格：" +
		// goods.getPrice() + ",购买数量：" + goods.getNum());
		// }

		File file = new File("F:\\manager\\goods");
		File[] files = file.listFiles();

		System.out.println("*******************原仓库商品信息************************");
		for (int i = 0; i < files.length; i++) {
			FileInputStream fis = new FileInputStream("F:\\manager\\goods\\" + files[i].getName());
			ObjectInputStream ois = new ObjectInputStream(fis);

			Goods goods = (Goods) ois.readObject();

			System.out.println("商品编号：" + goods.getId() + "\t 商品名称:" + goods.getGoodName() + "\t 商品价格："
					+ goods.getPrice() + "\t 商品数量：" + goods.getNum());

			fis.close();
			ois.close();
		}
		System.out.println("系统自动修改商品库存信息!");

		// 循环遍历接受到的购物车， 然后遍历当中 通过名字 取得文件的对象进行判断
		for (int i = 0; i < cart.list.size(); i++) {

			Goods goods = cart.list.get(i);
			FileInputStream fis = new FileInputStream("F:\\manager\\goods\\" + goods.getGoodName() + ".dat");
			ObjectInputStream ois = new ObjectInputStream(fis);

			Goods good = (Goods) ois.readObject();// 从文件中读取到
													// 商品对象根据goods.getGoodName()
													// 路径名

			if (good.getNum() != goods.getNum()) {
				int newnum = good.getNum() - goods.getNum();
				FileOutputStream fos = new FileOutputStream("F:\\manager\\goods\\" + goods.getGoodName() + ".dat");
				ObjectOutputStream oos = new ObjectOutputStream(fos);

				oos.writeObject(new Goods(good.getId(), good.getGoodName(), good.getPrice(), newnum));
				oos.close();
				fos.close();
			}
			fis.close();
			ois.close();
		}
		System.out.println("*******************现仓库商品信息************************");
		for (int i = 0; i < files.length; i++) {
			FileInputStream fis = new FileInputStream("F:\\manager\\goods\\" + files[i].getName());
			ObjectInputStream ois = new ObjectInputStream(fis);

			Goods goods = (Goods) ois.readObject();

			System.out.println("商品编号：" + goods.getId() + "\t 商品名称:" + goods.getGoodName() + "\t 商品价格："
					+ goods.getPrice() + "\t 商品数量：" + goods.getNum());

			fis.close();
			ois.close();
		}
	}

	// 管理员 手动修改数量
	@Override
	public void change() throws Exception {// 手动修改商品数量

		String n;
		File file = new File("F:\\manager\\goods");
		File[] files = file.listFiles();

		for (int i = 0; i < files.length; i++) {
			Scanner sc = new Scanner(System.in);
			FileInputStream fis = new FileInputStream("F:\\manager\\goods\\" + files[i].getName());
			ObjectInputStream ois = new ObjectInputStream(fis);
			Goods goods = (Goods) ois.readObject();
			System.out.println("请问您需要修改商品" + goods.getGoodName() + "的数量么？（a，修改  b 不修改）");
			n = sc.next();
			if ("a".equals(n)) {
				FileOutputStream fos = new FileOutputStream("F:\\manager\\goods\\" + goods.getGoodName() + ".dat");
				ObjectOutputStream oos = new ObjectOutputStream(fos);
				System.out.print("请输入你要修改的数量:");
				int num = sc.nextInt();
				oos.writeObject(new Goods(goods.getId(), goods.getGoodName(), goods.getPrice(), num));
				oos.close();

			} else {
				continue;
			}

			ois.close();
		}
	}

}
